package com.juphoon.oauth.controller;

import com.juphoon.oauth.core.entity.ServerResponse;
import com.juphoon.oauth.core.listener.AuthenticationListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author rongbin.huang
 * @create 2018-06-29 下午10:22
 **/
@RestController
public class LogoutController {

    @Autowired
    private AuthenticationListener authenticationListener;

    @Autowired
    @Qualifier("consumerTokenServices")
    private ConsumerTokenServices consumerTokenServices;


    @PostMapping("/logout")
    public ServerResponse logout(HttpServletRequest request) {
        authenticationListener.logout(request);
        String authorization = request.getHeader("Authorization");
        String token = authorization.replace("bearer ", "");
        consumerTokenServices.revokeToken(token);
        return ServerResponse.SUCCESS();
    }

}
